package top.csjss.hospital.controller;

import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.csjss.hospital.dto.SysUserDTO;
import top.csjss.hospital.dto.SysUserUpdataDTO;
import top.csjss.hospital.entity.SysUser;
import top.csjss.hospital.enums.HospitalExceptionEnums;
import top.csjss.hospital.service.SysUserService;
import top.csjss.hospital.vo.R;
import top.csjss.hospital.vo.SysUserVO;
import top.csjss.hospital.vo.UserRoleVO;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author ztf
 * @Date 9:55 2021/10/18
 **/
@RestController
public class UserController {

    private Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/user/selectAllUser")
    public R selectAllUser(String realname, String loginname,String address, String identity, String phone
            , @RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "1")Integer limit){
        //查询所有用户信息
        PageInfo<SysUser> pageInfo=sysUserService.selectAllUser(realname,loginname,address,identity,phone,page,limit);
        //获取总数据数
        Long count = pageInfo.getTotal();
        //获取展示数据
        List<SysUser>sysUserList = pageInfo.getList();
        List<SysUserVO> data=new ArrayList<>();
        for (SysUser sysUser : sysUserList) {
            SysUserVO vo=new SysUserVO();
            BeanUtils.copyProperties(sysUser, vo);
            vo.setPwd("");
            data.add(vo);
        }
        //响应数据
        return R.ok(count, data);
    }

    /**
     * 添加用户
     * @param sysUserDTO
     * @param bindingResult
     * @return
     */
    @PostMapping("/user/addUser")
    public R addUser(@Valid SysUserDTO sysUserDTO, BindingResult bindingResult){
        //校验参数
        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【添加用户信息】 参数不合法 msg={}",msg);
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR.getCode(), msg);
        }
        //封装实体类
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDTO, sysUser);
        //调用service保存
        sysUserService.addUser(sysUser);

        //返回
        return R.ok("添加用户信息成功");

    }

    /**
     * 修改用户
     * @param sysUserUpdataDTO
     * @param bindingResult
     * @return
     */
    @PostMapping("user/updateUser")
    public R updateUser(@Valid SysUserUpdataDTO sysUserUpdataDTO, BindingResult bindingResult){
        //校验参数
        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【修改用户信息】参数不合法",msg);
            return R.error(HospitalExceptionEnums.USER_UPDATA_ERROR, msg);
        }
        //封装实体类
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserUpdataDTO, sysUser);
        //调用service
        sysUserService.updataUser(sysUser);

        return R.ok("修改用户信息成功");
    }
    @PostMapping("/user/resetUserPwd")
    public R resetUserPwd(Integer userid){
        if (userid==null||userid<0){
            log.info("【重置密码】 请求参数异常");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"【重置密码】 请求参数异常");
        }
        sysUserService.resetUserPwd(userid);

        return R.ok("密码重置成功");
    }

    /**
     * 删除用户
     * @param userid
     * @return
     */
    @PostMapping("/user/deleteUser")
    public R deleteUser(Integer userid){
        if (userid==null||userid<0){
            log.info("【删除用户】 请求参数异常");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"【删除用户】 请求参数异常");
        }
        sysUserService.deleteUserByUserId(userid);
        return R.ok("用户删除成功");
    }

    /**
     * 分配角色信息（展示用户角色信息）
     * @param userid
     * @return
     */
    @GetMapping("/user/initUserRole")
    public R initUserRole(Integer userid){
        //校验参数
        if (userid==null || userid<=0){
            log.info("【分配角色信息】参数不合法");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR);
        }
        //调用service
        List<UserRoleVO> data= sysUserService.initUserRole(userid);
        //响应
        return R.ok(data);
    }

    /**
     * 分配角色信息（修改用户角色）
     * @param userid
     * @param ids
     * @return
     */
    @PostMapping("/user/saveUserRole")
    public R saveUserRole(Integer userid,Integer ids[]){
        //校验参数
        if (userid==null || userid<=0){
            log.info("【分配角色信息】参数不合法");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR);
        }
        //调用service分配用户角色
        sysUserService.saveUserRole(userid,ids);
        //响应
        return R.ok("角色分配成功");
    }

}
